# -*- coding: utf-8 -*-
# keywords_heat.py
# Created by Hardy on 24th, Feb
# Copyright 2017 杭州网川教育科技有限公司. All rights reserved.

from querier.esquerier import ElasticSearchQuerier
from utils.province_code import CUSTOMER_TYPE


class PopularKolQuerier(ElasticSearchQuerier):
    def __init__(self, commu_analysis_multi_query, wechat_kol_info_query):
        super(PopularKolQuerier, self).__init__(None, None, None)
        self.commu_analysis_multi_query = commu_analysis_multi_query
        self.wechat_kol_info_query = wechat_kol_info_query

    def search(self, args):
        res = []
        communicate_data = self.commu_analysis_multi_query.search(args)['data']
        for word_data in communicate_data:
            keyword = word_data['keyword'][0]
            biz_list = word_data['res']['biz_list']
            biz_doc = word_data['res']['biz_doc']
            date = word_data['res']['date']
            all_count = word_data['res']['all_count']
            a = {"biz_codes": biz_list}

            kol_infos = self.wechat_kol_info_query.search(a)['infos']
            biz_names = []
            scores = []
            head_imgs = []
            customer_type_list = []
            verified_list = []
            position_list = []
            for biz in biz_list:
                for d in kol_infos:
                    if d.get('biz_code') == biz:
                        name = d.get('biz_name', '未知')
                        biz_names.append(name)
                        score = d.get('score', 0)
                        scores.append(score)
                        head_img = d.get('head_img', ' ')
                        head_imgs.append(head_img)
                        customer_type = CUSTOMER_TYPE.get(d.get('customer_type'))
                        customer_type_list.append(customer_type)
                        position = self.get_position(d.get('position'))
                        position_list.append(position)
                        verified = self.get_verified(d.get('verify_date'))
                        verified_list.append(verified)
                    else:
                        continue

            score_distribute = {}
            for score in scores:
                if score <= 500:
                    score_distribute['0-500'] = score_distribute.get('0-500', 0) + 1
                elif 500 < score <= 1000:
                    score_distribute['500-1000'] = score_distribute.get('500-1000', 0) + 1
                elif 1000 < score <= 1500:
                    score_distribute['1000-1500'] = score_distribute.get('1000-1500', 0) + 1
                else:
                    score_distribute['1500+'] = score_distribute.get('1500+', 0) + 1

            verified_dict = self.to_dict(verified_list)

            customer_type_dict = self.to_dict(customer_type_list)
            position_dict = self.to_dict(position_list)

            kv = {
                "keyword": keyword,
                "date": date,
                'all_count': all_count,
                'biz_codes': biz_list,
                'biz_names': biz_names,
                'YKQ': scores,
                'head_imgs': head_imgs,
                'biz_doc': biz_doc,
                'YKQ_distribute': score_distribute,
                'verified_dict': verified_dict,
                'customer_type_dict': customer_type_dict,
                'position_dict': position_dict
            }
            res.append(kv)
        return {"data": res}

    def _build_query(self, args):
        pass

    def _build_result(self, es_result, param):
        pass

    @staticmethod
    def get_verified(v_date):
        if v_date is not None and v_date != "":
            res = '认证'
        else:
            res = '未认证'
        return res

    @staticmethod
    def to_dict(lis):
        le = len(lis)
        b = [1] * le
        d = {}
        for k, v in zip(lis, b):
            d[k] = d.get(k, 0) + v
        return d

    @staticmethod
    def get_position(pos):
        if pos == "Null" or pos == "null" or pos is None:
            res = '未知'
        else:
            res = pos
        return res
